package com.kinyx.framework.file;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Test;

public class KPropertiesReader_Tester_ {

	@Test
	public void test() {
		final Properties properties = KPropertiesReader.read("test.properties");

		this.没有配置属性取默认值(properties);

		this.配置的属性值为空白取默认值(properties);

		this.配置的属性值为null取null值(properties);

		this.配置的属性值(properties);
	}

	@SuppressWarnings("unchecked")
	private void 没有配置属性取默认值(final Properties properties) {
		Assert.assertEquals("", KPropertiesReader.getStringValue(properties, "not configed", ""));
		Assert.assertEquals(new Byte((byte) 0), KPropertiesReader.getByteValue(properties, "not configed", (byte) 0));
		Assert.assertEquals(new Short((short) 0), KPropertiesReader.getShortValue(properties, "not configed", (short) 0));
		Assert.assertEquals(new Integer(0), KPropertiesReader.getIntegerValue(properties, "not configed", 0));
		Assert.assertEquals(new Long(0), KPropertiesReader.getLongValue(properties, "not configed", 0L));
		Assert.assertEquals(new Float(0), KPropertiesReader.getFloatValue(properties, "not configed", 0f));
		Assert.assertEquals(new Double(0), KPropertiesReader.getDoubleValue(properties, "not configed", 0d));
		Assert.assertEquals(new Boolean(false), KPropertiesReader.getBooleanValue(properties, "not configed", false));
		Assert.assertEquals(KPropertiesReader_Tester_.class, KPropertiesReader.getClassValue(properties, "not configed", KPropertiesReader_Tester_.class));

		Assert.assertEquals(Collections.EMPTY_LIST, KPropertiesReader.getListValue(properties, "not configed", Collections.EMPTY_LIST, null));
		Assert.assertEquals(Collections.EMPTY_MAP, KPropertiesReader.getMapValue(properties, "not configed", Collections.EMPTY_MAP, null, null));
	}

	@SuppressWarnings("unchecked")
	private void 配置的属性值为空白取默认值(final Properties properties) {
		Assert.assertEquals("", KPropertiesReader.getStringValue(properties, "blankValue", ""));
		Assert.assertEquals(new Byte((byte) 0), KPropertiesReader.getByteValue(properties, "blankValue", (byte) 0));
		Assert.assertEquals(new Short((short) 0), KPropertiesReader.getShortValue(properties, "blankValue", (short) 0));
		Assert.assertEquals(new Integer(0), KPropertiesReader.getIntegerValue(properties, "blankValue", 0));
		Assert.assertEquals(new Long(0), KPropertiesReader.getLongValue(properties, "blankValue", 0L));
		Assert.assertEquals(new Float(0), KPropertiesReader.getFloatValue(properties, "blankValue", 0f));
		Assert.assertEquals(new Double(0), KPropertiesReader.getDoubleValue(properties, "blankValue", 0d));
		Assert.assertEquals(new Boolean(false), KPropertiesReader.getBooleanValue(properties, "blankValue", false));
		Assert.assertEquals(KPropertiesReader_Tester_.class, KPropertiesReader.getClassValue(properties, "blankValue", KPropertiesReader_Tester_.class));

		Assert.assertEquals(Collections.EMPTY_LIST, KPropertiesReader.getListValue(properties, "blankValue", Collections.EMPTY_LIST, null));
		Assert.assertEquals(Collections.EMPTY_MAP, KPropertiesReader.getMapValue(properties, "blankValue", Collections.EMPTY_MAP, null, null));
	}

	@SuppressWarnings("unchecked")
	private void 配置的属性值为null取null值(final Properties properties) {
		Assert.assertEquals(null, KPropertiesReader.getStringValue(properties, "nullValue", ""));
		Assert.assertEquals(null, KPropertiesReader.getByteValue(properties, "nullValue", (byte) 0));
		Assert.assertEquals(null, KPropertiesReader.getShortValue(properties, "nullValue", (short) 0));
		Assert.assertEquals(null, KPropertiesReader.getIntegerValue(properties, "nullValue", 0));
		Assert.assertEquals(null, KPropertiesReader.getLongValue(properties, "nullValue", 0L));
		Assert.assertEquals(null, KPropertiesReader.getFloatValue(properties, "nullValue", 0f));
		Assert.assertEquals(null, KPropertiesReader.getDoubleValue(properties, "nullValue", 0d));
		Assert.assertEquals(null, KPropertiesReader.getBooleanValue(properties, "nullValue", false));
		Assert.assertEquals(null, KPropertiesReader.getClassValue(properties, "nullValue", KPropertiesReader_Tester_.class));

		Assert.assertEquals(null, KPropertiesReader.getListValue(properties, "nullValue", Collections.EMPTY_LIST, null));
		Assert.assertEquals(null, KPropertiesReader.getMapValue(properties, "nullValue", Collections.EMPTY_MAP, null, null));
	}

	@SuppressWarnings("unchecked")
	private void 配置的属性值(final Properties properties) {
		Assert.assertEquals("str", KPropertiesReader.getStringValue(properties, "getStringValued", ""));
		Assert.assertEquals(new Byte((byte) 3), KPropertiesReader.getByteValue(properties, "getByteValued", (byte) 0));
		Assert.assertEquals(new Short((short) 3), KPropertiesReader.getShortValue(properties, "getShortValued", (short) 0));
		Assert.assertEquals(new Integer(3), KPropertiesReader.getIntegerValue(properties, "getIntegerValued", 0));
		Assert.assertEquals(new Long(3), KPropertiesReader.getLongValue(properties, "getLongValued", 0L));
		Assert.assertEquals(new Float(3), KPropertiesReader.getFloatValue(properties, "getFloatValued", 0f));
		Assert.assertEquals(new Double(3), KPropertiesReader.getDoubleValue(properties, "getDoubleValued", 0d));
		Assert.assertEquals(new Boolean(true), KPropertiesReader.getBooleanValue(properties, "getBooleanValued", false));
		Assert.assertEquals(KPropertiesReader.class, KPropertiesReader.getClassValue(properties, "getClassValued", KPropertiesReader_Tester_.class));

		final List<?> list1 = KPropertiesReader.getListValue(properties, "list1", Collections.EMPTY_LIST, null);
		Assert.assertTrue(list1.get(0).equals("foo"));
		Assert.assertTrue(list1.get(1).equals("bar"));
		Assert.assertTrue(list1.get(2).equals("qux"));

		final List<?> list2 = KPropertiesReader.getListValue(properties, "list2", Collections.EMPTY_LIST, "&");
		Assert.assertTrue(list2.get(0).equals("foo"));
		Assert.assertTrue(list2.get(1).equals("bar"));
		Assert.assertTrue(list2.get(2).equals("qux"));

		final Map<String, String> map1 = KPropertiesReader.getMapValue(properties, "map1", Collections.EMPTY_MAP, null, null);
		Assert.assertTrue(map1.get("foo").equals("1"));
		Assert.assertTrue(map1.get("bar").equals("2"));

		final Map<String, String> map2 = KPropertiesReader.getMapValue(properties, "map2", Collections.EMPTY_MAP, ",", "-");
		Assert.assertTrue(map2.get("foo").equals("1"));
		Assert.assertTrue(map2.get("bar").equals("2"));
	}

}
